<template>
  <div class="app-container">
    <el-row :gutter="20" class="mb8">
      <el-col :span="1.5">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-form-item label="预测模型">
                <el-select v-model="form.model" placeholder="请选择预测模型">
                  <el-option
                    v-for="dict in dict.type.rul_model"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-form-item label="训练数据" v-if="form.model != null">
                <el-upload
                  class="upload-demo"
                  ref="upload"
                  action="#"
                  :on-preview="handlePreview"
                  :on-remove="handleRemove"
                  :before-remove="beforeRemove"
                  :http-request="uploadHttpRequest"
                  multiple
                  :limit="1"
                  :on-exceed="handleExceed"
                  :on-change="uploadFiles"
                  :file-list="fileList">
                  <el-button size="small" type="primary">点击上传</el-button>
                  <div slot="tip" class="el-upload__tip">只支持csv/mat格式文件</div>
                </el-upload>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-form-item label="模型名称" prop="name" v-if="form.model != null">
                <el-input
                  type="text"
                  placeholder="请输入模型名称"
                  v-model.trim="form.name"
                  maxlength="10"
                  show-word-limit
                  clearable
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-form-item label="惩罚系数" v-if="form.model==='0'">
                <el-input
                  type="text"
                  placeholder="请输入内容"
                  v-model="form.c"
                  maxlength="10"
                  show-word-limit
                  clearable
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-form-item label="核系数" v-if="form.model==='0'">
                <el-input
                  type="text"
                  placeholder="请输入内容"
                  v-model="form.r"
                  maxlength="10"
                  show-word-limit
                  clearable
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-form-item label="Epoch" prop="epoch" v-if="form.model!=null & form.model!=='0'">
                <el-input
                  type="text"
                  placeholder="请输入epoch"
                  v-model="form.epoch"
                  maxlength="10"
                  show-word-limit
                  clearable
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-form-item label="Batch" v-if="form.model!=null & form.model!=='0'">
                <el-input
                  type="text"
                  placeholder="请输入batch size"
                  v-model="form.batch"
                  maxlength="10"
                  show-word-limit
                  clearable
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-form-item label="学习率" v-if="form.model!=null & form.model!=='0'">
                <el-input
                  type="text"
                  placeholder="请输入学习率"
                  v-model="form.lr"
                  maxlength="10"
                  show-word-limit
                  clearable
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-form-item label="优化器" v-if="form.model!=null & form.model!=='0'">
                <el-select v-model="form.optimizer" placeholder="请选择优化器">
                  <el-option
                    v-for="dict in dict.type.optimizer_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-form-item label="隐藏层数" v-if="form.model!=null & form.model!=='0'">
                <el-input
                  type="text"
                  placeholder="请输入隐藏层数"
                  v-model="form.layer"
                  maxlength="10"
                  show-word-limit
                  clearable
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-col>
      <el-col :span="6" :offset='4'>
        <el-image
          style="width: 350px; height:350px"
          :src="require('@/assets/images/r0.png')" v-if="form.model==='0'"/>
        <el-image
          style="width: 350px; height:350px"
          :src="require('@/assets/images/r1.png')" v-if="form.model==='1'"/>
        <el-image
          style="width: 500px; height: 500px"
          :src="require('@/assets/images/r2.png')" v-if="form.model==='2'"/>
      </el-col>
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm" v-if="form.model != null">模型训练</el-button>
      <el-button @click="cancel" v-if="form.model != null">取 消</el-button>
    </div>
  </div>
</template>

<script>
  import {delInfo, getInfo, listInfo} from "@/api/bearing/basic";
  import {setDiagParam} from "../../../api/bearing/train";

  export default {
    name: "RUL",
    dicts: ['rul_model', 'optimizer_type'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 部门表格数据
        infoList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          bid: null,
        },
        fileList: [],
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name: [
            {required: true, message: "请输入模型名称", trigger: "blur"}
          ],
          epoch: [
            {required: true, message: "请输入训练轮数", trigger: "blur"}
          ]
        }
      };
    },
    created() {
    },
    methods: {
      /** 查询部门列表 */
      getList() {
        this.loading = true;
        listInfo(this.queryParams).then(response => {
          this.infoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        //this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          model: null,
          files: null,
          delFlag: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null
        };
        this.fileList = null;
        this.$refs.upload.clearFiles();
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加部门";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getInfo(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改部门";
        });
      },
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePreview(file) {
        console.log(file);
      },
      uploadHttpRequest(data) {
      },
      handleExceed(files, fileList) {
        this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
      },
      beforeRemove(file, fileList) {
        return this.$confirm(`确定移除 ${file.name}？`);
      },
      uploadFiles(item, fileList) {
        this.form.files = fileList
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除部门编号为"' + ids + '"的数据项？').then(function () {
          return delInfo(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.loading = true;
            let formData = new FormData();
            if ("undefined" === typeof (this.form.files)) {
              this.$modal.msgError("请上传训练文件");
              return;
            }
            formData.append('files', this.form.files[0].raw);
            for (let key in this.form) {
              if (key !== "files") {
                formData.append(key, this.form[key])
              }
            }
            setRulParam(formData).then(response => {
              this.infoList = response.rows;
              let msg = JSON.parse(response['msg'])['data'];
              if ("请检查文件格式" !== msg) {
                this.$modal.msgSuccess(msg);
              }
              this.total = response.total;
              this.loading = false;
            });
          }
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('bearing/managment/export', {
          ...this.queryParams
        }, `info_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
